package com.tl.commonlibrary.storage.filecache;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import java.io.File;

/**
 * Created by jiangzhongshan on 15/7/28.
 */
public class FileCacheTool {
    public static String getCacheDirectory(String path, String subFileName) {
        if (TextUtils.isEmpty(path) || TextUtils.isEmpty(subFileName)) {
            return null;
        }
        File file = new File(path, subFileName);
        if (file != null) {
            if (!file.exists()) {
                file.mkdirs();
            }
            return file.getPath();
        }
        return null;
    }

    public static String getCacheSubPath(String subFileName, Context context) {
        if (TextUtils.isEmpty(subFileName)) {
            return null;
        }
        if (context != null) {
            File cacheDir = context.getExternalCacheDir();
            return getCacheDirectory(cacheDir.getPath(), subFileName);
        }
        return null;
    }

    public static String getImgPath(Context context) {
        return getCacheSubPath("img", context);
    }

    public static String getVideoPath(Context context) {
        return getCacheSubPath("video", context);
    }

    /**
     * 永久存储的文件缓存地址
     * 除非用户手动删除或者刷机清空整个sd卡
     *
     * @return
     */
    public static String getCachePath() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return getCacheDirectory(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "FileCache/" + "forbidden", "ns");
        }
        return null;
    }

    public static FileCache newInstance(Context context) {
        FileCache fileCache = new FileCache(context);
        fileCache.open();
        return fileCache;
    }

    public static FileCache newInstance(File cachePathFile) {
        FileCache fileCache = new FileCache(cachePathFile);
        fileCache.open();
        return fileCache;
    }

    public static FileCache newInstance(CacheConfig cacheConfig) {
        FileCache fileCache = new FileCache(cacheConfig);
        fileCache.open();
        return fileCache;
    }

    public static void save(String key, String value, Context context) {
        FileCache fileCache = newInstance(context);
        fileCache.save(key, value);
        fileCache.close();
    }

    private static File createDirectory(String path) {
        if (TextUtils.isEmpty(path)) {
            return null;
        }
        File cachePathFile = new File(path);
        if (!cachePathFile.exists()) {
            cachePathFile.mkdirs();
        }
        return cachePathFile;
    }

    public static void save(String cachePath, String key, String value) {
        File cachePathFile = createDirectory(cachePath);
        if (cachePathFile != null) {
            FileCache fileCache = newInstance(cachePathFile);
            fileCache.save(key, value);
            fileCache.close();
        }
    }

    public static String load(String key, Context context) {
        FileCache fileCache = newInstance(context);
        String result = fileCache.load(key);
        fileCache.close();
        return result;
    }

    public static String load(String cachePath, String key, Context context) {
        File cachePathFile = createDirectory(cachePath);
        if (cachePathFile != null) {
            FileCache fileCache = newInstance(context);
            String result = fileCache.load(key);
            fileCache.close();
            return result;
        }
        return null;
    }

    public static void append(String key, String value, Context context) {
        FileCache fileCache = newInstance(context);
        fileCache.append(key, value);
        fileCache.close();
    }

    public static void append(String cachePath, String key, String value) {
        File cachePathFile = createDirectory(cachePath);
        if (cachePathFile != null) {
            FileCache fileCache = newInstance(cachePathFile);
            fileCache.append(key, value);
            fileCache.close();
        }
    }

    /**
     * 只能清除android/包名/的文件
     */
    public static void clearAll(Context context) {
        File file = context.getExternalCacheDir();
        FileTool.delete(file);
    }
}
